package dyy.zc.note.utils;


public class CodeFormatUtil {
    public static String getFormatString(String data) {
        String result = data.replace("{", "\n{\n").replace("}", "\n}\n").replace(";", ";\n").replace("\n\n", "\n");
        return getFormatString(result, 0);
    }

    /**
     * 用来换行格式
     */
    private static String getFormatString(String data, int leval) {
        String empty = "";
        empty += leval == 0 ? "" : "    ";
        data = data.replace("\n", "\n" + empty);
        int startPosition = data.indexOf("{");
        int endPosition = data.lastIndexOf("}");
        if (startPosition == -1 || endPosition == -1) {//表明已经没有需要缩进的人   //表示格式不正确
            return data;
        }
        String startData = data.substring(0, startPosition + 1);
        String endData = data.substring(endPosition, data.length());
        leval++;
        String middleData = getFormatString(data.substring(startPosition + 1, endPosition), leval);
        middleData = middleData.substring(0, middleData.length() - 2);
        return startData + middleData + endData;
    }
}
